package com.example.demo.controller.admin;

import com.example.demo.model.CategoryModel;
import com.example.demo.model.Json;
import com.example.demo.services.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RequestMapping("/back/category/")
@RestController
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @RequestMapping("list")
    public ModelAndView list(
            Model model,
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize
    ){
        PageInfo pageInfo = categoryService.getCategoryListFromDb(page, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return new ModelAndView("admin/category/list");
    }

    @RequestMapping("add")
    public ModelAndView add(
            Model model
    ){

        return new ModelAndView("admin/category/add");
    }

    @RequestMapping("addPost")
    public Json addPost(
            @RequestBody CategoryModel categoryModel
    )
    {
        try{
            int r = categoryService.add(categoryModel);
            if(r == 1) {
                return new Json().success("新增成功");
            } else {
                return new Json().error("新增失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

    @RequestMapping("edit")
    public ModelAndView edit(
            Model model,
            @RequestParam(name = "id", required = true) Long id
    ){
        CategoryModel categoryInfo = categoryService.getCategoryInfo(id);
        model.addAttribute("info", categoryInfo);
        return new ModelAndView("admin/category/edit");
    }

    @RequestMapping("editPost")
    public Json editPost(
            @RequestBody CategoryModel categoryModel
    ){
        try{
            boolean r = categoryService.edit(categoryModel);
            if(r) {
                return new Json().success("修改成功");
            } else {
                return new Json().error("修改失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

    @RequestMapping("del")
    public Json del(
            @RequestBody CategoryModel categoryModel
    ){
        try{
            boolean r = categoryService.del(categoryModel.getId());
            if(r) {
                return new Json().success("修改成功");
            } else {
                return new Json().error("修改失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }
}
